package com.kimi.game2048

import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import com.kimi.game2048.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private val viewModel: GameViewModel by viewModels()

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)


        // 观察游戏数据
        viewModel.tiles.observe(this, Observer { tiles ->
            binding.gridLayout.removeAllViews()
            tiles.forEach { tile ->
                val tileView = TileView(this)
                tileView.setValue(tile.value)
                binding.gridLayout.addView(tileView)
            }
        })

        // 重置游戏按钮
        binding.resetButton.setOnClickListener {
            viewModel.resetGame()
        }

    }
}